package com.ruoyi.web.controller.report;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.report.config.ReportConfig;
import com.ruoyi.report.domain.ReportTablebase;
import com.ruoyi.report.service.IReportTablebaseService;
import com.ruoyi.report.util.ReportUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 报告基础表格Controller
 * 
 * @author wry
 * @date 2023-06-20
 */
@RestController
@RequestMapping("/report/tableBase")
public class ReportTablebaseController extends BaseController
{
    @Autowired
    private IReportTablebaseService reportTablebaseService;

    /**
     * 查询报告基础表格列表
     */
    @PreAuthorize("@ss.hasPermi('report:tablebase:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReportTablebase reportTablebase)
    {
        ReportUtil report=new ReportUtil();
        report.doCreateReport(ReportConfig.getReportPath(),ReportConfig.getReportTpl()+ DateUtils.parseDateToStr("yyyy\\MM\\dd\\ss\\",new Date()));
       //S System.out.println(ReportConfig.getReportTpl());
        startPage();
        List<ReportTablebase> list = reportTablebaseService.selectReportTablebaseList(reportTablebase);
        return getDataTable(list);
    }

    /**
     * 导出报告基础表格列表
     */
    @PreAuthorize("@ss.hasPermi('report:tablebase:export')")
    @Log(title = "报告基础表格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReportTablebase reportTablebase)
    {
        List<ReportTablebase> list = reportTablebaseService.selectReportTablebaseList(reportTablebase);
        ExcelUtil<ReportTablebase> util = new ExcelUtil<ReportTablebase>(ReportTablebase.class);
        util.exportExcel(response, list, "报告基础表格数据");
    }

    /**
     * 获取报告基础表格详细信息
     */
    @PreAuthorize("@ss.hasPermi('report:tablebase:query')")
    @GetMapping(value = "/{tableId}")
    public AjaxResult getInfo(@PathVariable("tableId") Long tableId)
    {
        return success(reportTablebaseService.selectReportTablebaseByTableId(tableId));
    }
    /**
     * 查询报告展示表格列表
     */
    @GetMapping("/tableCombo")
    public AjaxResult tableCombo()
    {
        return success(reportTablebaseService.selectReportTablebaseList(new ReportTablebase()));
    }

    /**
     * 新增报告基础表格
     */
    @PreAuthorize("@ss.hasPermi('report:tablebase:add')")
    @Log(title = "报告基础表格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReportTablebase reportTablebase)
    {
        if (!reportTablebaseService.checkReportTablebaseUnique(reportTablebase))
        {
            return error("新增表格'" + reportTablebase.getTableName() + "'失败，表格名称已存在");
        }
        reportTablebase.setCreateBy(getUsername());
        return toAjax(reportTablebaseService.insertReportTablebase(reportTablebase));
    }

    /**
     * 修改报告基础表格
     */
    @PreAuthorize("@ss.hasPermi('report:tablebase:edit')")
    @Log(title = "报告基础表格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReportTablebase reportTablebase)
    {
        if (!reportTablebaseService.checkReportTablebaseUnique(reportTablebase))
        {
            return error("修改表格'" + reportTablebase.getTableName() + "'失败，表格名称已存在");
        }
        reportTablebase.setUpdateBy(getUsername());
        return toAjax(reportTablebaseService.updateReportTablebase(reportTablebase));
    }

    /**
     * 删除报告基础表格
     */
    @PreAuthorize("@ss.hasPermi('report:tablebase:remove')")
    @Log(title = "报告基础表格", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tableIds}")
    public AjaxResult remove(@PathVariable Long[] tableIds)
    {
        return toAjax(reportTablebaseService.deleteReportTablebaseByTableIds(tableIds));
    }

}
